// pages/center/center.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 菜单栏
    navbarList: [{
      id: 214,
      iconUrl: '/images/admin/center/8.png',
      label: '反馈中心',
      path: '/pages/admin/plugins/feedback/list/index'
    }, {
      id: 252,
      iconUrl: '/images/admin/center/8.png',
      label: '反馈中心',
      path: '/pages/admin/plugins/superWorkOrder/list/index'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '个人中心',
    });
    this.setData({
      ['config.version']: app.globalData.config['version']
    })

    let navbarListTemp = [];
    let navbarList = this.data.navbarList;
    let pluginList = wx.getStorageSync('pluginList');
    for (let j in navbarList) {
      for (let i in pluginList) {
        if ((pluginList[i]['auth_id'] === navbarList[j]['id'] && pluginList[i]['display'] === 1) || navbarList[j]['id'] === 0) {
          navbarListTemp.push(navbarList[j]);
          break;
        }
      }
    }
    console.log(navbarListTemp);

    this.setData({
      navbarList: navbarListTemp
    })
  },

  // 跳转页面
  bindJumpToPage(e) {
    var page = e.currentTarget.dataset.page;
    app.jumpToPage(page.path, page.label);
  },

  getUserInfo: function() {
    var that = this;
    var data = {
      point_url: app.server.adminApi.getUsersInfo,
      openid: app.globalData.openid
    }
    app.server.getConnect(data).then(function(res) {
      wx.setStorageSync("userInfo", res.data);
      app.globalData.userInfo = res.data;
      that.setData({
        work_status: res.data.work_status,
        wechat_pic: res.data.wechat_pic,
        position: res.data.position,
        name: res.data.name,
        branch_name: res.data.branch_name,
        bonus: res.data.bonus
      })
    })
  },

  doReception: function() {
    var that = this;
    wx.showActionSheet({
      itemList: ['接待', '不接待'],
      success: function(res) {
        console.log(res.tapIndex);
        if (res.tapIndex != that.data.work_status) {
          that.updateWorkStatus(res.tapIndex);
        }
      }
    })
  },

  updateWorkStatus: function(status) {
    var that = this;
    var data = {
      point_url: app.server.adminApi.updateWorkStatus,
      admin_id: app.globalData.userInfo.id,
      work_status: status
    };

    app.server.getConnect(data).then(function(res) {
      console.log(res);
      wx.showToast({
        title: '操作成功',
        mask: true
      });
      app.globalData.userInfo.work_status = status;
      wx.setStorageSync('userInfo', app.globalData.userInfo);
      that.setData({
        work_status: status
      })
    }, function(res) {
      app.showConfirm('提示', res.data.msg, false);
    })
  },

  toSignIn: function() {
    wx.navigateTo({
      url: 'sign-in/sign-in',
    })
  },

  toWallet: function() {
    wx.navigateTo({
      url: 'wallet/wallet?bonus=' + this.data.bonus,
    })
  },

  toFeedBack: function() {
    wx.navigateTo({
      url: '/pages/admin/plugins/feedback/list/index',
    })
  },

  toSuperWorkOrder: function() {
    wx.navigateTo({
      url: '/pages/admin/plugins/superWorkOrder/list/index',
    })
  },

  toPersonalInfo: function() {
    wx.navigateTo({
      url: 'personal-info/personal-info',
    })
  },

  toAboutUs: function() {
    wx.navigateTo({
      url: 'about-us/about-us',
    })
  },

  toSetting: function() {
    wx.navigateTo({
      url: '/pages/admin/center/setting/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.utils.initAdminWxappStyle(app, this);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})